<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="药品名称" prop="drugName"
                :rules="[
                  {required: true, message: '药品名称不能为空', trigger: 'blur'}
                 ]">
              <el-input v-model="inputForm.drugName" placeholder="请填写药品名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="药品功效" prop="drugEfficacy"
                :rules="[
                  {required: true, message: '药品功效不能为空', trigger: 'blur'}
                 ]">
              <el-input v-model="inputForm.drugEfficacy" placeholder="请填写药品功效"     ></el-input>
           </el-form-item>
        </el-col>

        <el-col :span="12">
            <el-form-item label="适用人群" prop="applicablePersonnel"
                :rules="[
                  {required: true, message: '适用人群不能为空', trigger: 'blur'}
                 ]">
              <el-input v-model="inputForm.applicablePersonnel" placeholder="请填写适用人群"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="单价" prop="unitPrice"
                :rules="[
                  {validator: validator.isNumber, trigger:'blur',required: true,message: '单价不能为空'}
                 ]">
              <el-input v-model="inputForm.unitPrice" placeholder="请填写单价"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="生产商" prop="manufacturer"
                :rules="[
                 ]">
              <el-input v-model="inputForm.manufacturer" placeholder="请填写生产商"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="口服剂量" prop="oralDose"
                :rules="[
                  { trigger:'blur',required: true,message: '口服剂量不能为空'}
                 ]">
              <el-input v-model="inputForm.oralDose" placeholder="请填写口服剂量"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="口服频率" prop="oralFrequency"
                :rules="[
                   { trigger:'blur',required: true,message: '口服频率不能为空'}
                 ]">
              <el-input v-model="inputForm.oralFrequency" placeholder="请填写口服频率"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import drugInfoService from '@/api/drug/drugInfoService'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          drugName: '',
          drugEfficacy: '',
          drugIngredient: '',
          applicablePersonnel: '',
          unitPrice: '',
          manufacturer: '',
          oralDose: '',
          oralFrequency: ''
        }
      }
    },
    components: {
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建药品信息`
        } else if (method === 'edit') {
          this.title = '修改药品信息'
        } else if (method === 'view') {
          this.title = '查看药品信息'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            drugInfoService.queryById(this.inputForm.id).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            drugInfoService.save(this.inputForm).then(({data}) => {
              this.visible = false
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>


